package com.mvc.demo.controller;

import com.mvc.demo.bean.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * @author H
 * @date 2023/1/5
 */
@RestController
@RequestMapping(value = "test")
public class TestMvcController {

    @RequestMapping("noAnnotation")
    public String noAnnotation(String username, Integer age) {
        System.out.println(username);
        System.out.println(age);
        return "success";
    }

    @RequestMapping("annotation")
    public String annotation(@RequestParam(name = "userName") String username, Integer age) {
        System.out.println(username);
        System.out.println(age);
        return "success";
    }

    @RequestMapping("requestArray")
    public String requestArray(String[] strArray) {
        Arrays.stream(strArray).forEach(System.out::println);
        return "success";
    }

    @PostMapping("requestBody")
    public String requestBody(@RequestBody(required = false) User user) {
        System.out.println(user.getUsername());
        System.out.println(user.getAge());
        System.out.println(user.getRegin());
        return "success";
    }

    @PostMapping("pathVariable/{id1}")
    public String pathVariable(@PathVariable(name = "id1") String id) {
        System.out.println(id);
        return "success";
    }

    @GetMapping("dateFormat")
    public String dateFormat(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        System.out.println(date);
        return "success";
    }

    @GetMapping("dateFormat1")
    public String dateFormat1(User user) {
        System.out.println(user.getUsername());
        System.out.println(user.getAge());
        System.out.println(user.getDate());
        return "success";
    }

    @GetMapping("getUser")
    public User getUser() {
        User user = new User();
        user.setUsername("小明");
        user.setAge(21);
        user.setDate(new Date());
        return user;
    }

}
